package work.huangxin.share.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.huangxin.share.basemapper.SameService;
import work.huangxin.share.domain.other.CommentMessage;
import work.huangxin.share.mapper.ArticleMessageMapper;
import work.huangxin.share.mapper.CommentMessageMapper;
import work.huangxin.share.util.RedisUtil;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class CommentService extends SameService<CommentMessage> {

    @Resource
    private CommentMessageMapper commentMessageMapper;

    @Resource
    private ArticleMessageMapper articleMessageMapper;

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 获取评论
     */
    public PageInfo getCommentById(Integer pageNumber, Integer articleId) {
//        PageInfo pageInfo = (PageInfo) redisUtil.get(RedisConstant.articleById + articleId);
//        ArticleMessage articleMessage = (ArticleMessage) redisUtil.get(RedisConstant.articleById + articleId);
//        if (!Objects.isNull(pageInfo)) {
//            articleMessage.setTotalComment((int)pageInfo.getTotal());
//            redisUtil.expire(RedisConstant.articlePage + pageNumber, 60);
//            //TODO 异步
//            threadPoolExecutor.execute(() -> {
//                int count = articleMessageMapper.updateByPrimaryKey(articleMessage);
//                if (count <= 0) {
//                    throw new RuntimeException("评论总数更新失败");
//                }
//            });
//            return pageInfo;
//        }
        //如果redis中不存在，则在数据库中查找,拿到commentMessage
//        PageInfo pageInfoResult = new PageInfo(commentMessageMapper.getCommentById(articleId));
        PageHelper.startPage(pageNumber, 10);
        PageInfo<CommentMessage> pageInfoResult = PageInfo.of(commentMessageMapper.getCommentById(articleId));
//        //将评论数的total，放入到 articleMessage
//        articleMessage.setTotalComment((int)pageInfoResult.getTotal());
//        //再将 articleMessage 持久化 到数据库中
//        articleMessageMapper.updateByPrimaryKey(articleMessage);
//        //将数据库中的数据 存入到 redis 中
//        redisUtil.set(RedisConstant.articlePage + pageNumber, pageInfoResult, 60);
        //返回
        return pageInfoResult;

    }

    /**
     * 获取评论的人信息以及内容
     */


    public CommentMessage getCommentMessage(Integer commentId) {


        return commentMessageMapper.getCommentMessage(commentId);
    }

    public Integer getTotalComment(Integer articleId) {
        return commentMessageMapper.getTotalComments(articleId);
    }

    //得到 articleId
    public Integer getArticleId(Integer commentId) {
        return commentMessageMapper.getArticleId(commentId);
    }

    //得到对应 commentId中的回复评论的评论数
    public Integer getReplyComment(Integer commentId) {
        return commentMessageMapper.getReplyComment(commentId);
    }
}
